package com.huiyujinrong.qiangdanbao.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.huiyujinrong.qiangdanbao.R;
import com.huiyujinrong.qiangdanbao.bean.SportHeaderBean;
import com.huiyujinrong.qiangdanbao.bean.SportItemBean;

import java.util.List;

/**
 * 精彩活动adapter
 * Created by ZQXQ-Developer on 2017/6/28.
 */

public class SportAdapter extends ListBaseAdapter {
    private List<Object> items;
    private Context mContext;
    public OnItemClickListener mItemClickListener;
    private LayoutInflater inflater;

    public SportAdapter setmItemClickListener(OnItemClickListener mItemClickListener) {
        this.mItemClickListener = mItemClickListener;
        return this;
    }

    public SportAdapter(Context context, List<Object> items) {
        this.mContext = context;
        this.items = items;
        inflater = LayoutInflater.from(context);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = inflater.inflate(viewType, parent, false);
        if (viewType == R.layout.item_sport_header) {
            return new HeaderViewHolder(view, mItemClickListener);
        } else {
            return new EImageViewHolder(view, mItemClickListener);
        }
    }

    @Override
    public int getItemViewType(int position) {
        Object object = items.get(position);
        if (object instanceof SportHeaderBean) {
            return R.layout.item_sport_header;
        } else {
            return R.layout.item_sport;
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof HeaderViewHolder) {
            ((HeaderViewHolder) holder).bind((SportHeaderBean) items.get(position));
        } else {
            ((EImageViewHolder) holder).bind((SportItemBean) items.get(position));
        }
    }

    @Override
    public int getItemCount() {
        return items == null ? 0 : items.size();
    }


    public interface OnItemClickListener {
        void onItemClickListener(View view);

        void onHeaderClickListener(View view);
    }

    /**
     * 展业神器
     */
    private class EImageViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView eImageView;
        private TextView textView;
        private OnItemClickListener clickListener;
        private LinearLayout linearLayout;

        public EImageViewHolder(View view, OnItemClickListener mItemClickListener) {
            super(view);
            linearLayout = (LinearLayout) view.findViewById(R.id.llSport);
            eImageView = (ImageView) view.findViewById(R.id.imSport);
            textView = (TextView) view.findViewById(R.id.tvInfo);
            this.clickListener = mItemClickListener;
            linearLayout.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (clickListener != null) {
                clickListener.onItemClickListener(view);
            }
        }

        public void bind(SportItemBean eImage) {
            textView.setText(eImage.getInfo());
            Glide.with(mContext).load(eImage.getUrl()).into(eImageView);
        }
    }

    private class HeaderViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView imageView;
        private OnItemClickListener clickListener;

        public HeaderViewHolder(View view, OnItemClickListener mItemClickListener) {
            super(view);
            imageView = (ImageView) view.findViewById(R.id.imSportHeader);
            this.clickListener = mItemClickListener;
            imageView.setOnClickListener(this);
        }

        public void bind(SportHeaderBean sportHeaderBean) {
            Glide.with(mContext).load(sportHeaderBean.getUrl()).into(imageView);
        }

        @Override
        public void onClick(View view) {
            if (clickListener != null) {
                clickListener.onHeaderClickListener(view);
            }
        }
    }
}